package com.yanqu.road.server.cmd.house;

import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserHouseBar;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.HousePb;
import com.yanqu.road.pb.house.HouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.HouseModule;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Protocol.S_HOUSE_DISPATCH_PATRONS, desc = "派遣门客到柜台")
public class HouseBarDispatchCmd extends AbstractCommand {

	private int operate;

	private Map<Integer,Integer> posPatronsMap = new ConcurrentHashMap<>();

	//pos-patronsId
	private Map<String,Property> rewardMap = new ConcurrentHashMap<>();

	private int num = 0;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		HouseProto.HouseDispatchPatronsReqMsg reqMsg = HouseProto.HouseDispatchPatronsReqMsg.parseFrom(packet.getBytes());
		operate = reqMsg.getOperate();
		posPatronsMap = HousePb.parseHouseDispatchTempMsg(reqMsg);
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		Map<Integer,UserHouseBar> userHouseBarMap = player.getModule(HouseModule.class).getUserHouseBarMap();
		List<Integer> patronsIdList = new ArrayList<>();

        if (!SystemOpenMgr.systemOpen(player, eSystemId.HouseBank.getValue())) {
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }

		for(UserHouseBar userHouseBar : userHouseBarMap.values()) {
			if(userHouseBar.getPatronsId() != 0){
				patronsIdList.add(userHouseBar.getPatronsId());
			}
		}

		Map<Integer, UserPatrons> userPatronsMap = player.getModule(PatronsModule.class).getUserPatronsMap();
		for(Integer patronsId : posPatronsMap.values()){
			if(!userPatronsMap.containsKey(patronsId)){
				ret = GameErrorCode.E_PATRONS_NO_FOUND;
				log.error("userId {},patronsId:{} not found",player.getUserId(),patronsId);
				return;
			}
		}


		for(Integer pos : posPatronsMap.keySet()) {
			if (!userHouseBarMap.containsKey(pos)) {
				ret = GameErrorCode.E_HOUSE_BAR_POSITION_ERROR;
				return;
			}
			UserHouseBar userHouseBar = userHouseBarMap.get(pos);
			if(operate == 1 && userHouseBar.getPatronsId() != 0){
				ret = GameErrorCode.E_HOUSE_DISPATCH_HAS_PATRONS;
				return;
			}

			if(operate == 2 && userHouseBar.getPatronsId() == 0){
				ret = GameErrorCode.E_HOUSE_DISPATCH_NO_PATRONS;
				return;
			}

			if(operate == 1 && patronsIdList.contains(posPatronsMap.get(pos))){
				ret = GameErrorCode.E_HOUSE_BAR_PATRONS_IS_DISPATCH;
				return;
			}
		}

		if(operate == 1){
			if(posPatronsMap.size() > 1 && userHouseBarMap.size() < GameConfig.HOUSE_BANK_ONE_KEY_DISPATCH_LIMIT){
				ret = GameErrorCode.E_HOUSE_BANK_ONE_KEY_DISPATCH_LIMIT;
				return;
			}
			num = player.getModule(HouseModule.class).dispatchPatrons(posPatronsMap);
		}else if(operate == 2){
			if(posPatronsMap.size() > 1 && userHouseBarMap.size() < GameConfig.HOUSE_BANK_ONE_KEY_RECALL_LIMIT){
				ret = GameErrorCode.E_HOUSE_BANK_ONE_KEY_RECALL_LIMIT;
				return;
			}
			rewardMap = player.getModule(HouseModule.class).recallPatrons(posPatronsMap);
		}
		player.getModule(HouseModule.class).syncUserHouseBank();
	}

	@Override
	public void sendData(GamePlayer player) {
		HouseProto.HouseDispatchPatronsRespMsg.Builder respMsg = HouseProto.HouseDispatchPatronsRespMsg.newBuilder();
		respMsg.setRet(ret);
		for(String posAndPatronsId : rewardMap.keySet()){
			List<Integer> intList = StringUtils.stringToIntegerList(posAndPatronsId,"-");
			Property reward = rewardMap.get(posAndPatronsId);
			respMsg.addRewardList(HousePb.parseHouseBarRewardTempMsg(intList.get(0),intList.get(1),reward));
		}
		respMsg.setNum(num);
		player.sendPacket(Protocol.U_HOUSE_DISPATCH_PATRONS, respMsg);
	}

}
